﻿using System;
using System.Collections.Generic;

namespace Vkontakte.Api.Operations
{
	[ApiMethod("getProfiles")]
	public sealed class ProfilesGet
		:Base30ApiOperation
	{
		private Person[] _result = null;

		private IList<long> _uids = null;
		private IList<string> _domains = null;

		public ProfilesGet()
		{
			
		}

		protected override void Compile()
		{
			AddParameter("method", "getProfiles");
			if (_uids!=null)
			{
				if (_uids.Count==0)
				{
					throw new ApplicationException("uid не указаны");
				}
				string strUids = _uids[0].ToString();
				for (int i=1;i<_uids.Count;i++)
				{
					strUids += "," + _uids[i];
				}
				AddParameter("uids", strUids);
			}

			if (_domains != null)
			{
				if (_domains.Count == 0)
				{
					throw new ApplicationException("domains не указаны");
				}
				string strDomains = _domains[0];
				for (int i = 1; i < _domains.Count; i++)
				{
					strDomains += "," + _domains[i];
				}
				AddParameter("domains", strDomains);
			}

			if (NameCase != NameCase.Nom)
			{
				AddParameter("name_case", NameCase.ToStr());
			}
			if (PersonProperties != PersonProperties.None)
			{
				AddParameter("fields", PersonProperties.ToStr());
			}

			base.Compile();
		}

		protected override void ParseData(string data)
		{
			ListingResponce responce = Serializer.LoadObjectXmlString<ListingResponce>(data);
			if (responce.Persons==null)
			{
				return;
			}
			for (int i = 0; i < responce.Persons.Length; i++)
			{
				responce.Persons[i].NickName = responce.Persons[i].NickName.FixXmlString();
				_result = responce.Persons;
			}
			base.ParseData(data);
		}

		public IList<long> Uids
		{
			get
			{
				if (_domains!=null)
				{
					throw new ApplicationException("Одновременное использование uid и domain name недопустимо");
				}
				if (_uids==null)
				{
					_uids=new List<long>();
				}
				return _uids;
			}
		}

		public IList<string> Domains
		{
			get
			{
				if (_uids!=null)
				{
					throw new ApplicationException("Одновременное использование uid и domain name недопустимо");
				}
				if (_domains == null)
				{
					_domains = new List<string>();
				}
				return _domains;
			}
		}

		public NameCase NameCase { get; set; }

		public PersonProperties PersonProperties { get; set; }

		public Person[] GetResult()
		{
			return _result;
		}
	}
}